from fastapi import APIRouter, Depends
from sqlalchemy.ext.asyncio import AsyncSession
from database.database import get_db
from core.base.api_mode import ApiResponse, api_success
from core.pagination import DependsPagination, PageData
from modules.system.schemas.staff_schemas import StaffCreate, StaffUpdate
from modules.system.params.staff_params import StaffQuery
from modules.system.services.staff_service import StaffService
from modules.system.vos.staff_vo import StaffResponse


router = APIRouter()


@router.post("/create", response_model=ApiResponse[StaffResponse], summary="创建业务员")
async def create_staff(payload: StaffCreate, db: AsyncSession = Depends(get_db)):
    staff = await StaffService.create_staff(db, payload)
    return api_success(data=staff, msg="创建成功")


@router.post(
    "/list",
    response_model=ApiResponse[PageData[StaffResponse]],
    summary="获取业务员列表",
    dependencies=[DependsPagination],
)
async def list_staffs(query_params: StaffQuery, db: AsyncSession = Depends(get_db)):
    result = await StaffService.get_staff_with_pagination(db, query_params)
    return api_success(data=result, msg="获取成功")


@router.put("/{staff_id}", response_model=ApiResponse[StaffResponse], summary="更新业务员")
async def update_staff(staff_id: int, payload: StaffUpdate, db: AsyncSession = Depends(get_db)):
    staff = await StaffService.update_staff(db, staff_id, payload)
    return api_success(data=staff, msg="更新成功")


@router.delete("/{staff_id}", response_model=ApiResponse[dict], summary="删除业务员")
async def delete_staff(staff_id: int, db: AsyncSession = Depends(get_db)):
    await StaffService.delete_staff(db, staff_id)
    return api_success({"message": f"业务员ID {staff_id} 删除成功"}, msg="删除成功")